<?php

/**
 * 启动记录(测试)
 *
 * @author cdlei
 */

require_once 'CommonController.php';

class Admin_ClientBootTestController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_ClientBootTest();
        $this->_sourceModel = new Application_Model_Source();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $time = time();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['udid'] = trim($this->_request->get('udid'));
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        $vars['sourceSubId'] = trim($this->_request->get('sourceSubId'));
        $vars['ip'] = trim($this->_request->get('ip'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $urls = $vars;

        $wheres[] = "`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."'";
        if ($vars['udid'] != '') {
            $wheres[] = "`udid` = '" . addslashes($vars['udid']) . "'";
        }
        if ($vars['osName'] != '') {
            $wheres[] = "`osName` = '" . addslashes($vars['osName']) . "'";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '" . addslashes($vars['sourceId']) . "'";
        }
        if ($vars['sourceSubId'] != '') {
            $wheres[] = "`sourceSubId` = '" . addslashes($vars['sourceSubId']) . "'";
        }
        if ($vars['ip'] != '') {
            $wheres[] = "`ip` = '" . addslashes($vars['ip']) . "'";
        }
        if ($vars['startTime'] != '') {
            $wheres[] = "`addTime` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`addTime` <= '{$vars['endTime']}' ";
        }

        $orderBy = "`addTime` DESC, `id` DESC";

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * 
        FROM `{$this->_model->getTableName()}` {$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        $this->view->sourceModel = $this->_sourceModel;
        
    }
}